﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Data;
using System.IO;

namespace Daneas.Utility.Universals
{
    /// <summary>
    /// xml操作
    /// </summary>
    public class XMLs
    {        
        /// <summary>
        /// //用DataTable生成xml内容字符串
        /// </summary>
        /// <param name="xmlDS"></param>
        /// <returns></returns>
        public static string ConvertDataTableToXML(DataTable xmlDS)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            try
            {
                stream = new MemoryStream();
                writer = new XmlTextWriter(stream, Encoding.GetEncoding("utf-8"));
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);
                return System.Text.Encoding.GetEncoding("utf-8").GetString(arr).Trim();
            }
            catch
            {
                return String.Empty;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }
        
        /// <summary>
        /// //将xml生成一个DataSet
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public static DataSet ConvertXMLToDataSet(string xmlData)
        {
            StreamReader stream = null;
            //XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StreamReader(xmlData);
                // reader = new XmlTextReader(stream);
                //xmlDS.ReadXml(stream);
                xmlDS.ReadXml(xmlData);
                return xmlDS;
            }
            catch (Exception ex)
            {
                string strTest = ex.Message;
                return null;
            }
            finally
            {
                // if (reader != null)
                //    reader.Close();
            }
        }

        /// <summary>
        /// 从xml文本读取
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static string XMLToJsonFromDocument(string xml)
        {
            string json = string.Empty;

            return json;
        }

        /// <summary>
        /// 从xml路径读取,这个方法只有读取一个对象
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string XMLToJsonFromPath(string path)
        {
            string json = string.Empty;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(path);

            if (!doc.HasChildNodes)
            {
                return json;
            }

            XmlNode root = doc.FirstChild;

            if (!root.HasChildNodes)
            {
                return json;
            }

            json += "{json:[";

            foreach (XmlNode node in root.ChildNodes)
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    json += "{";
                    json += child.Name + ",";
                    json += child.Value;
                    json += "},";
                }
            }

            json.TrimEnd(',');

            json += "]}";

            return json;
        }

        /// <summary>
        /// 将一个实体对象写入xml
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="path">xml存放路径</param>
        /// <returns></returns>
        public static bool WriteToXML<T>(T obj, string path)
        {
            Type type = obj.GetType();


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "gb2312", "yes");

            xmldoc.AppendChild(dec);
            XmlElement root = xmldoc.CreateElement("root");
            xmldoc.AppendChild(root);
            //创建列表
            XmlNode node = xmldoc.CreateElement("UserList");
            root.AppendChild(node);
            //创建信息列表
            XmlNode node2 = xmldoc.CreateElement("MessageList");
            root.AppendChild(node2);

            PropertyInfo[] propertys = type.GetProperties();

            foreach (PropertyInfo property in propertys)
            {
                //property.GetValue(obj,property.);
            }

            return false;
        }
    }
}
